package top.songxs.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.songxs.yygh.common.exp.DiyException;
import top.songxs.yygh.common.result.R;
import top.songxs.yygh.hosp.service.HospitalSetService;
import top.songxs.yygh.model.hosp.HospitalSet;
import top.songxs.yygh.vo.hosp.HospitalSetQueryVo;

import java.util.Date;
import java.util.List;

//医院设置接口
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")

@Slf4j
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //Feign接口调用方法
    @GetMapping("getApiUrl/{hoscode}")
    public String getApiUrl(@PathVariable("hoscode") String hoscode){

        HospitalSet hospitalSet = hospitalSetService.getByHosCode(hoscode);
        return hospitalSet.getApiUrl();

    }



    //查询所有医院设置
    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list", list);
    }


    @ApiOperation(value = "医院设置删除")
    @DeleteMapping("{id}")
    public R deleteById(@ApiParam(name = "id", value = "ID", required = true)
                        @PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        return b ? R.ok() : R.error();
    }

    @ApiOperation(value = "医院设置批量删除")
    @DeleteMapping("deleteByIds")
    public R deleteByIds(@RequestBody List<Long> ids) {
        boolean b = hospitalSetService.removeByIds(ids);
        return b ? R.ok() : R.error();
    }

    @ApiOperation(value = "医院设置保存新增")
    @PostMapping("saveHospSet")
    public R saveHospSet(
            @RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        hospitalSet.setCreateTime(new Date());
        hospitalSet.setUpdateTime(new Date());
        boolean save = hospitalSetService.save(hospitalSet);
        return save ? R.ok() : R.error();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("getHospSet/{id}")
    public R getById(
            @ApiParam(name = "id", value = "ID", required = true)
            @PathVariable("id") String id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    //带条件的分页查询
    @ApiOperation(value = "带条件的分页查询")
    @PostMapping("findPageCondition/{page}/{limit}")
    public R getPage(@PathVariable("page") Integer page,
                     @PathVariable("limit") Integer limit,
                     @RequestBody HospitalSetQueryVo hospitalSetQueryVo) {
        //分页
        Page<HospitalSet> iPage = new Page<>(page, limit);
        //构造查询条件  注意条件判空
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();

        if (!StringUtils.isEmpty(hoscode))
            wrapper.eq("hoscode", hoscode);

        if (!StringUtils.isEmpty(hosname))
            wrapper.like("hosname", hosname);

        //调用service方法进行查询
        hospitalSetService.page(iPage, wrapper);

        List<HospitalSet> list = iPage.getRecords();
        long total = iPage.getTotal();

        return R.ok().data("rows", list).data("total", total);
    }

    @PostMapping("updateHospSet")
    @ApiOperation(value = "根据id进行修改")
    public R updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean update = hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    // 更新用户的状态，1、正常 0、锁定
    // 需求：判断status值得范围（0，1），如果不在范围抛出自定义异常
    // 判断是否重复操作，如果是，抛出一个自定义异常
    @PutMapping("updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        if (status != 1 && status != 0)//判断状态的的范围
            throw new DiyException(20001, "status的取值范围不正确");

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet.getStatus() == status)//判断是否重复操作
            throw new DiyException(20001, "重复操作");

        hospitalSet.setStatus(status);
        hospitalSet.setUpdateTime(new Date());//最后更新的时间
        hospitalSetService.updateById(hospitalSet);

        return R.ok();

    }

}

























